#include "stdafx.h"
#include "qoccalgo.h"
#include <BRepPrimAPI_MakePrism.hxx>
#include <TopExp_Explorer.hxx>
#include <TopoDS.hxx>
#include <BRepFilletAPI_MakeFillet.hxx>
#include <BRepOffsetAPI_MakeThickSolid.hxx>
#include <BRepAlgoAPI_Fuse.hxx>
#include <BRepAlgoAPI_Cut.hxx>
#include <BRepAlgoAPI_Common.hxx>
#include <BRepPrimAPI_MakeRevol.hxx>
#include <BRepBuilderAPI_Transform.hxx>

QOccAlgo::QOccAlgo(QObject *parent)
	: OccObjectBase(parent)
{

}

QOccAlgo::~QOccAlgo()
{

}

QVariant QOccAlgo::MakeRevol(const TopoDS_Shape& S, const gp_Pnt& pntOrign, const gp_Vec& V, const ZMReal & fAngle)
{
	gp_Ax1 axis;
	axis.SetLocation(pntOrign);
	axis.SetDirection(gp_Dir(V));
	return ToVariant(BRepPrimAPI_MakeRevol(S,axis,fAngle).Shape());
}

QVariant QOccAlgo::MakePrism(const TopoDS_Shape& S, const gp_Vec& V)
{
	return ToVariant(BRepPrimAPI_MakePrism(S, V).Shape());
}

QVariant QOccAlgo::GetAllFace(const TopoDS_Shape& S)
{
	TopExp_Explorer anFaceExplorer(S, TopAbs_FACE);
	ZMVector<TopoDS_Face> vData;
	while (anFaceExplorer.More()) {
		TopoDS_Face anFace = TopoDS::Face(anFaceExplorer.Current());
		//Add edge to fillet algorithm
		vData.push_back(anFace);
		anFaceExplorer.Next();
	}
	return ToVariant(vData);
}

QVariant QOccAlgo::GetAllEdge(const TopoDS_Shape& S)
{
	TopExp_Explorer anEdgeExplorer(S, TopAbs_EDGE);
	ZMVector<TopoDS_Edge> vData;
	while (anEdgeExplorer.More()) {
		TopoDS_Edge anFace = TopoDS::Edge(anEdgeExplorer.Current());
		//Add edge to fillet algorithm
		vData.push_back(anFace);
		anEdgeExplorer.Next();
	}
	return ToVariant(vData);
}

QVariant QOccAlgo::MakeFillet(const TopoDS_Shape& S, const QVariant& vEdge, const ZMReal& fRadius)
{
	ZMVector<TopoDS_Edge> lsEdge = FromVariant<TopoDS_Edge>(vEdge);
	BRepFilletAPI_MakeFillet fillet(S);
	for (const TopoDS_Edge& edge:lsEdge)
	{
		fillet.Add(fRadius, edge);
	}
	return ToVariant(fillet.Shape());
}
QVariant QOccAlgo::MakeThickSolid(const TopoDS_Shape& S, const QVariant& vClosingFaces, const ZMReal& Offset, const ZMReal& Tol)
{
	ZMVector<TopoDS_Face> lsFace = FromVariant<TopoDS_Face>(vClosingFaces);
	TopTools_ListOfShape facesToRemove;
	for (const TopoDS_Face& face : lsFace)
	{
		facesToRemove.Append(face);
	}
	return ToVariant(BRepOffsetAPI_MakeThickSolid(S, facesToRemove, Offset, Tol).Shape());
}

QVariant QOccAlgo::BoolUnion(const TopoDS_Shape& S0, const TopoDS_Shape& S1)
{
	return ToVariant(BRepAlgoAPI_Fuse(S0, S1).Shape());
}

QVariant QOccAlgo::BoolSub(const TopoDS_Shape& S0, const TopoDS_Shape& S1)
{
	return ToVariant(BRepAlgoAPI_Cut(S0, S1).Shape());
}

QVariant QOccAlgo::BoolCommon(const TopoDS_Shape& S0, const TopoDS_Shape& S1)
{
	return ToVariant(BRepAlgoAPI_Common(S0, S1).Shape());
}

QVariant QOccAlgo::Tansform(const TopoDS_Shape& S, const gp_Trsf& tran)
{
	TopoDS_Shape S1 = BRepBuilderAPI_Transform(S, tran).Shape();
	return ToVariant(S1);
}
